package com.auto.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auto.entity.Request;
import com.auto.rep.RequestRep;
import com.google.common.collect.Lists;

@Controller
public class RequestController {

	@Autowired
	private RequestRep requestRep;

	@RequestMapping(value = "request", method = RequestMethod.GET)
	@ResponseBody
	public List<Request> getRequests() {
		return Lists.newArrayList(requestRep.findAll());
	}

	@RequestMapping(value = "request/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Request getRequest(@PathVariable("id") Long id) {
		return requestRep.findOne(id);
	}

	@RequestMapping(value = "request/{id}", method = RequestMethod.POST)
	@ResponseBody
	public void updateRequest(@PathVariable("id") Long id, Request request) {
		request.setId(id);
		requestRep.save(request);
	}

	@RequestMapping(value = "request/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRequest(@PathVariable("id") Long id) {
		requestRep.delete(id);
	}

	@RequestMapping(value = "request", method = RequestMethod.POST)
	@ResponseBody
	public Request addRequest(Request request) {
		return requestRep.save(request);
	}
}
